package com.hirisun.calendar.gantt;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import org.apache.xmlbeans.XmlException;

import com.hirisun.calendar.filter.ChineseFestivalFilter;
import com.hirisun.calendar.filter.FestivalDay;
import com.hirisun.calendar.filter.GregorianFestivalFilter;
import com.hirisun.calendar.filter.IVocationFilter;
import com.hirisun.calendar.filter.TimeSlice;
import com.hirisun.calendar.filter.WeakendFilter;
import com.hirisun.calendar.filter.WorkingHours;

public class FilterXMLBean {

	private String filename = null;
	FiltersDocument document;
	private ArrayList<IVocationFilter> filters;
	private WorkingHours workingHours;

	public FilterXMLBean(String filename) {
		super();
		this.filename = filename;
	}

	public void aquireConfig() throws XmlException, IOException {
		File xmlFile = new File(filename);
		document = FiltersDocument.Factory.parse(xmlFile);
	}

	public WorkingHours getWorkingHours() {
		if (workingHours == null) {

			workingHours = new WorkingHours();
			WorkingHoursType hours = document.getFilters().getWorkingHours();

			PeriodType[] periods = hours.getPeriodArray();
			TimeSlice timeSlice;

			for (int i = 0; i < periods.length; i++) {
				timeSlice = new TimeSlice();

				timeSlice.setStartHour(periods[i].getFrom().getHour());
				timeSlice.setStartMinute(periods[i].getFrom().getMinute());
				timeSlice.setEndHour(periods[i].getTo().getHour());
				timeSlice.setEndMinute(periods[i].getTo().getMinute());
				workingHours.addTimeSlice(timeSlice);
			}
		}

		return workingHours;
	}

	public ArrayList<IVocationFilter> getFilters() throws XmlException,
			IOException {
		if (filters == null) {
			filters = new ArrayList<IVocationFilter>();
			WeakendType weakend = document.getFilters().getWeakend();

			IVocationFilter filter = null;

			filter = new WeakendFilter();
			((WeakendFilter) filter).setWeaks(weakend.getWeakArray());

			filters.add(filter);

			FestivalType[] festivals = document.getFilters().getFestivalArray();

			FestivalDay festivalDay = null;
			for (int i = 0; i < festivals.length; i++) {
				if (festivals[i].getType() == TypeEnumType.CHINESE) {
					filter = new ChineseFestivalFilter();
					FestivalDayType[] festivalDays = festivals[i]
							.getFestivalDayArray();

					for (int j = 0; j < festivalDays.length; j++) {
						festivalDay = new FestivalDay();
						festivalDay.setMonth(festivalDays[j].getMonth());
						festivalDay.setDay(festivalDays[j].getDay());
						((ChineseFestivalFilter) filter)
								.addFestival(festivalDay);
					}

					filters.add(filter);
				}
				if (festivals[i].getType() == TypeEnumType.GREGORIAN) {
					filter = new GregorianFestivalFilter();
					FestivalDayType[] festivalDays = festivals[i]
							.getFestivalDayArray();

					for (int j = 0; j < festivalDays.length; j++) {
						festivalDay = new FestivalDay();
						festivalDay.setMonth(festivalDays[j].getMonth());
						festivalDay.setDay(festivalDays[j].getDay());
						((GregorianFestivalFilter) filter)
								.addFestival(festivalDay);
					}
					filters.add(filter);
				}
			}
		}
		return filters;
	}

}
